package org.serverct.finalfight.internal.game.hadesgame;

import org.bukkit.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.hadesgame.EventType;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.GameState;
import org.serverct.finalfight.util.CustomSettings;
import org.serverct.finalfight.util.StringPair;

import java.util.UUID;

public abstract class ContinuousEvent extends BasicEvent {
    public int duration;
    public int ticking;

    public ContinuousEvent(BasicGame game, EventType type) {
        super(game, type);
        this.game = game;
        this.type = type;
        this.duration = CustomSettings.config.getInt("duration." + type.name());
        this.ticking = 0;
    }

    @Override
    public void execute() {
        if (this instanceof Listener) {
            Bukkit.getPluginManager().registerEvents((Listener) this, FinalFight.plugin);
        }

        this.start();
        //TODO Customize
        this.bar = Bukkit.createBossBar(this.getBarTitle(), BarColor.PINK, BarStyle.SOLID, BarFlag.PLAY_BOSS_MUSIC);
        this.bar.setProgress(1.0);
        this.bar.removeFlag(BarFlag.PLAY_BOSS_MUSIC);
        for (UUID uuid : this.game.players.keySet()) {
            this.bar.addPlayer(Bukkit.getPlayer(uuid));
        }

        new BukkitRunnable() {
            @Override
            public void run() {
                ContinuousEvent.this.tick();

                ContinuousEvent.this.bar.setTitle(ContinuousEvent.this.getBarTitle());
                ContinuousEvent.this.bar.setProgress(1.0 - (double) ContinuousEvent.this.ticking / ContinuousEvent.this.duration);

                if (ContinuousEvent.this.ticking >= ContinuousEvent.this.duration) {
                    ContinuousEvent.this.bar.removeAll();
                    ContinuousEvent.this.end();
                    this.cancel();
                    ContinuousEvent.this.unregister();
                }
                ContinuousEvent.this.ticking++;

                if (ContinuousEvent.this.game.state != GameState.RUNNING) {
                    ContinuousEvent.this.bar.removeAll();
                    ContinuousEvent.this.unregister();
                    this.cancel();
                }

            }
        }.runTaskTimer(FinalFight.plugin, 0L, 20L);
    }

    public void start() {

    }

    public void end() {

    }

    public void tick() {

    }

    public String getBarTitle() {
        return CustomSettings.getLanguageWithoutPrefix("randomeventbossbar",
                new StringPair("%event%", this.type.getName(this.factor)),
                new StringPair("%left%", String.valueOf(this.duration - this.ticking)));
    }

    public void unregister() {
        if (this instanceof Listener) {
            HandlerList.unregisterAll((Listener) this);
        }
    }
}
